package com.br4mmie.asciistory.ui;

import java.io.IOException;
import java.util.ArrayList;
import com.br4mmie.asciistory.ASCIIStoryActivity;
import com.br4mmie.asciistory.CharImage;


/**
 * 
 * Window Handler class
 * Handles all window functions.
 * Manages windows in list and determines what windows are active and wich need to be closed
 */
public class WindowHandler{
	
	private ArrayList<Window> list;//List that contains all windows
	private ArrayList<Window> newWindows; //contains windows to be added on next loop
	/**
	 * Constructor, all thats needed is the arraylist.
	 */
	public WindowHandler()
	{
		list = new ArrayList<Window>();
		newWindows = new ArrayList<Window>();
	}
	
	/**
	 * Adds windows to the handler
	 * @param w = window to add
	 */
	public void add(Window w)
	{
		newWindows.add(w);
	}
	
	private void transferNewWindows()
	{
		if(newWindows.isEmpty())return;
		for(Window w : newWindows)
		{
			list.add(w);
		}
		newWindows.clear();
	}
	/**
	 * Loops through the windows, first deactivating all windows and determining what windows need to be closed
	 * then it removes the closed windows and activates the currently selected window
	 * also calls the loop function for all windows.
	 */
	private void loopWindows()
	{
		transferNewWindows();		
		if(list.isEmpty())return;
		ArrayList<Window> rem = new ArrayList<Window>();
		
		for(Window w : list){
			w.deactivate();
			
			if(w.isClosed)rem.add(w);
			if(list.isEmpty())return;
		}
		
		for(Window w : rem)
		{
			list.remove(w);
		}
		if(list.isEmpty())return;
		list.get(list.size()-1).activate();
		
		for(Window w : list)
		{
			w.onLoop();
		}
		
		
		
	}
	
	public int getNumberOfWindows()
	{
		return list.size();
	}
	
	/**
	 * Removes the given window from the handler
	 * @param w = window to remove
	 */
	public void remove(Window w)
	{
		list.remove(w);
	}
	
	/**
	 * handles all window events, then loops through the windows to handle their events
	 * @return
	 */
	public boolean handleEvents()
	{
		if(list.isEmpty())return false;
		
		/*if(false)
		{
			OptionWindow.SelectionListener gameMenuListener = new OptionWindow.SelectionListener()
			{
				public void select(int option) 
				{
					try
					{
						switch (option)
						{
						case 1:
							break;
							
						case 2:
							ASCIIStoryActivity.getProfileManager().saveGame(ASCIIStoryActivity.getProfileManager().getProfileName()+".root.savegame", true);
							break;
						
						case 3:
							ASCIIStoryActivity.getProfileManager().saveGame(ASCIIStoryActivity.getProfileManager().getProfileName()+".root.savegame", true);
							ASCIIStoryActivity.getGameLoop().setupMainMenu();
							break;							
						
						case 4:
							ASCIIStoryActivity.getGameLoop().setupMainMenu();
							break;
						}
					}
					catch (IOException ex)
					{
						ex.printStackTrace();
					}
				}
			};
			OptionWindow menu = new OptionWindow(0, 0, ASCIIStoryActivity.getScreenWidth(), ASCIIStoryActivity.getScreenHeight(), "Game Menu", gameMenuListener);
			menu.addOption("Continue");
			menu.addOption("Save");
			menu.addOption("Save and Quit");
			menu.addOption("Quit without saving");
		}*/
		
		
		for(Window w : list)
		{
			if(w.active())w.handleEvents();
		}
		
		//!!!! 'C' vervangen voor cycle toets (TAB??) evt. pauze toevoegen
		
		return true;
	}
	
	/**
	 * Calls the draw functions for the windows.
	 * @param target = CharImage to draw to
	 */
	public void draw(CharImage target)
	{
		if(list.isEmpty())return;
		for(Window w : list)w.draw(target);
	}
	
	/**
	 * basic loop function
	 */
	public void onLoop()
	{
		loopWindows();
	}
	
	/**
	 * CycleWindows function.
	 * Cycles through the windows, determining wich one is active and on top
	 */
	public void cycleWindows()
	{
		Window tempWindow = list.get(0);
		list.remove(0);
		list.add(tempWindow);
	}

}
